﻿#include"LoginView.h"
#include"XQDatabaseConnectionView.h"
#include"XQCreateUserView.h"
#include"XQAlgorithm.h"
#include"XQEventFilterObject.h"
LoginView::LoginView(QWidget* parent)
:XQLoginView(parent)
{

}

XQDatabaseConnectionView* LoginView::databaseView()
{
	return m_databaseView;
}

void LoginView::pressDatabase()
{
	if (m_databaseView==nullptr)
	{
		m_databaseView = new XQDatabaseConnectionView;
		m_databaseView->init();
		m_databaseView->installEventFilter(new XQEventFilterObject([this] 
			{
				setEnabled(true);
			},QEvent::Hide,m_databaseView));
		connect(this, &QObject::destroyed,m_databaseView,&QObject::deleteLater);
		connect(m_databaseView, &XQDatabaseConnectionView::connectionSignals, this, &LoginView::databaseConnectionSignals);
		connect(m_databaseView, &XQDatabaseConnectionView::saveSignals, this, &LoginView::databaseSaveSignals);
		emit createDatabaseConnectionViewSignals(m_databaseView);
	}
	auto view=m_databaseView;
	// 设置窗口关闭时自动销毁
	//view->setAttribute(Qt::WA_DeleteOnClose);
	centerShow(view,this);
	setEnabled(false);
	
}

void LoginView::pressRegisterAccount()
{
	if (m_createAccount == nullptr)
	{
		m_createAccount = new XQCreateUserView;
		m_createAccount->init();
		m_createAccount->installEventFilter(new XQEventFilterObject([this]
			{
				setEnabled(true);
			}, QEvent::Hide, m_createAccount));
		connect(this, &QObject::destroyed, m_createAccount, &QObject::deleteLater);
		connect(m_createAccount, &XQCreateUserView::createAccountSignals, this, &LoginView::registerAccountSignals);
		connect(m_createAccount, &XQCreateUserView::sendVerifySignals, this, &LoginView::sendVerifySignals);
		emit createAccountViewSignals(m_createAccount);
	}
	auto view = m_createAccount;
	// 设置窗口关闭时自动销毁
	//view->setAttribute(Qt::WA_DeleteOnClose);
	centerShow(view, this);
	setEnabled(false);
}

void LoginView::init()
{
	XQLoginView::init();
	setRetrievePasswordVisible(false);
	connect(this,&XQLoginView::databaseSignals,this,&LoginView::pressDatabase);
	connect(this, &XQLoginView::registerAccountSignals,this,& LoginView::pressRegisterAccount);
}
